package com.jie.vo;
//统一返回类
import com.jie.enums.BizCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * <h1>通用响应对象定义</h1>
 * {
 *     "code": 0,
 *     "msg": "",
 *     "data": {}
 * }
 * */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResponse<T> implements Serializable {

    /**
     * 状态码 0 表示成功，1表示处理中，-1表示失败
     */
    private Integer code;
    /**
     * 数据
     */
    private Object data;
    /**
     * 描述
     */
    private String msg;

    /**
     * 成功，不传⼊数据
     *
     * @return
     */
    public static CommonResponse buildSuccess() {
        return new CommonResponse(200, null, "success");
    }

    /**
     * 成功，传⼊数据
     *
     * @param data
     * @return
     */
    public static CommonResponse buildSuccess(Object data) {
        return new CommonResponse(200, data, "success");
    }

    /**
     * 失败，传⼊描述信息
     *
     * @param msg
     * @return
     */
    public static CommonResponse buildError(String msg) {
        return new CommonResponse(-1, null, msg);
    }
    /**
     * ⾃定义状态码和错误信息
     *
     * @param code
     * @param msg
     * @return
     */
    public static CommonResponse<Object> buildCodeAndMsg(int code, String msg) {
        return new CommonResponse<Object>(code, null, msg);
    }

    /**
     * 传⼊枚举，返回信息
     *
     * @param codeEnum
     * @return
     */
    public static CommonResponse buildResult(BizCodeEnum codeEnum) {
        return CommonResponse.buildCodeAndMsg(codeEnum.getCode(), codeEnum.getMessage());
    }
}
